package tools;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.TreeSet;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import entities.Entity;
import entities.Query;
import entities.Result;

public class ResultOutput {
	
	private String resultDir;
	private String queryDir;
	private HashMap<String,Result> map;
	private int numberThreshold = -1;//limit the number of output 
	private float valueTreshold = -1;//limit the output based on entity's score
	public ResultOutput( String outputDir, String queryDir,ArrayList<Result> list){
		this.resultDir = outputDir;
		this.queryDir = queryDir;
		map = new HashMap<String,Result>();
		for( Result r:list){
			map.put(r.getQ().getId(), r);
		}
	}
	public void setNT(int nt){
		this.numberThreshold = nt;
	}
	
	public void setVt(float vt){
		this.valueTreshold = vt;
	}
	
	public void output_nt() throws Exception{
		File f = new File(this.queryDir);
		if(f.exists()){
			DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance(); 
			DocumentBuilder builder=factory.newDocumentBuilder(); 
			
			Document doc = builder.parse(f);
			
			NodeList nl = doc.getElementsByTagName("query");
			for( int i = 0;i<nl.getLength();i++){
				Node n = nl.item(i);
				Element element = (Element) n;
				String queryId = element.getAttribute("id");
				Result r = map.get(queryId);
				if ( r == null ){
					//throw new Exception("no such queryId in result set");
				}else{
					ArrayList<Entity> candidates = r.getCandidates();
					
					//System.out.println("query:"+r.getQ().getName()+" candidates:"+candidates.size());
					int j=0;
					for( Entity can: candidates){
						if( j> this.numberThreshold){
							break;
						}
						
						
						
						Element ne = doc.createElement("candidate");
						ne.setTextContent(can.getName());
						element.appendChild(ne);
						j++;
					}
				}
			    
			}
			
			DOMSource ds = new DOMSource(doc);
			  
			StreamResult sr = new StreamResult(new File(this.resultDir));
			  
			TransformerFactory.newInstance().newTransformer().transform(ds, sr);
			  
		}
	
	}
	
	public void output() throws Exception{
		File f = new File(this.queryDir);
		if(f.exists()){
			DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance(); 
			DocumentBuilder builder=factory.newDocumentBuilder(); 
			
			Document doc = builder.parse(f);
			
			NodeList nl = doc.getElementsByTagName("query");
			for( int i = 0;i<nl.getLength();i++){
				Node n = nl.item(i);
				Element element = (Element) n;
				String queryId = element.getAttribute("id");
				Result r = map.get(queryId);
				if ( r == null ){
					//throw new Exception("no such queryId in result set");
				}else{
					ArrayList<Entity> candidates = r.getCandidates();
					
					//System.out.println("query:"+r.getQ().getName()+" candidates:"+candidates.size());
					for( Entity can: candidates){
						Element ne = doc.createElement("candidate");
						ne.setTextContent(can.getName());
						element.appendChild(ne);
					}
				}
			    
			}
			
			DOMSource ds = new DOMSource(doc);
			  
			StreamResult sr = new StreamResult(new File(this.resultDir));
			  
			TransformerFactory.newInstance().newTransformer().transform(ds, sr);
			  
		}
		
	}
	


}
